#!/usr/bin/python 

import datetime
import sys
import string

if __name__ == '__main__':
    if(len(sys.argv) < 2):
	       raise Exception("Need file argument")

    file = open(sys.argv[1], "r")
    mins = 1
    if(len(sys.argv) > 2):
        mins = int(sys.argv[2])

    numHappy = 0
    numUnhappy = 0

    ''' use utc to avoid insanity '''
    now = datetime.datetime.utcnow()

    for line in file:
        words = string.split(line, "  ")
        timestamp = datetime.datetime.strptime(words[0].strip(), "%Y%m%dT%H%M%S")
        happiness = words[1].strip()
        delta = now  - timestamp
        if(delta.total_seconds() < mins*60):
            if(happiness == "h"):
                numHappy = numHappy + 1
            else:
                numUnhappy = numUnhappy + 1
            

    print "In the last ", mins , " minutes there were ", numHappy, " happy and ", numUnhappy, " unhappy"
    
    ''' avoid div by zero, assume we have no traffic but all is well.... valid? depends on sys size'''
    if(numHappy + numUnhappy == 0):
        sys.exit(0)
        
    proportionHappy = numHappy/(numHappy + numUnhappy)
    
    if(proportionHappy > 0.6):
        sys.exit(0)
    elif (proportionHappy < 0.5):
        sys.exit(2)
    else:
        sys.exit(1)
